package com.lafengmaker;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;

import com.lafengmaker.admin.entity.User;

public class MyibatisTool {
	static String BR="\r\n";
	static String BR2="\r\n\r\n";
	static String _T="\t";
	static String T2="\t\t";
	static String T3="\t\t\t";
	
	static String namesapce="admin";
	
	public static void main(String[] args) {
		try {
			MyibatisTool.createXMl(User.class);
			System.out.println("文件生成完成");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void createXMl(Class clzz) throws Exception{
		String dir="C:\\work\\worktemplete\\SpringS\\src\\main\\resources\\mybatis\\";
		String cname=clzz.getSimpleName();
		File f=new File(dir+namesapce);
		f.mkdirs();
		File xmlfile=new File(dir+namesapce+File.separator+cname+"Mapper.xml");
		StringBuilder sb= new StringBuilder();
		sb.append("<!DOCTYPE mapper 	PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" "+BR+ 
			"   \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">");
		sb.append(BR+BR+BR);
		sb.append("<mapper namespace=\"com.lafengmaker."+namesapce+".dao."+cname+"Dao\">");
		sb.append(BR2);
		sb.append(_T);
		sb.append("<sql id=\"selectAllColumns\">");
		sb.append(BR+_T+_T);
		sb.append("<![CDATA[");
		sb.append(BR+_T+_T);
		Field[] fs=clzz.getDeclaredFields();
		String quertcol="";
		for(Field fe:fs){
			quertcol+=","+fe.getName().toLowerCase()+BR+T2;
		}
		String tabnaeme="biz_"+cname.toLowerCase();
		String tabcol=quertcol.substring(1);
		quertcol=tabcol+" from "+tabnaeme;
		quertcol="select  "+quertcol;
		sb.append(quertcol);
		sb.append(BR+_T+_T);
		sb.append("]]>");
		sb.append(BR+_T);
		sb.append("</sql>");
		sb.append(BR);
		
		
		sb.append(BR2+_T);		
		sb.append("<sql id=\"where_fragement\">");
		sb.append(BR+T2);
		//sb.append("<![CDATA[");
		sb.append(" where 1=1 ");
		
		//Field[] fs=clzz.getDeclaredFields();
		//String quertcol="";
		for(Field fe:fs){
			sb.append(BR+T2);
			String fieldname=fe.getName();
			sb.append("<if test=\"entity."+fieldname+" != null and entity."+fieldname+" != ''\">");
			sb.append("and "+fieldname.toLowerCase()+" = #{entity."+fieldname+"}");
			sb.append("</if>");
		}
		
		sb.append(BR+_T);
		sb.append("</sql>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		
		sb.append(BR2+_T);		
		sb.append("<insert id=\"add\" parameterType=\"Object\">");
		sb.append(BR+T2);
		//sb.append("<![CDATA[");
		sb.append(" insert into  "+tabnaeme);
		sb.append("( "+tabcol+" )");
		sb.append(BR+T2);
		sb.append("values(");
		sb.append(BR+T2);
		
		//Field[] fs=clzz.getDeclaredFields();
		//String quertcol="";
		int i=0;
		for(Field fe:fs){
			sb.append(BR+T2);
			if(i>0){
				sb.append(",");
			}
			sb.append("#{"+fe.getName()+"}");
			i++;
		}
		sb.append(BR+T2+")");
		sb.append(BR+T2);
		sb.append("<selectKey resultType=\"java.lang.Integer\" keyProperty=\"id\"	order=\"AFTER\">");
		sb.append(BR+T3);
		sb.append("SELECT @@IDENTITY");
		sb.append(BR+T2);
		sb.append("</selectKey>");		
		sb.append(BR+_T);
		sb.append("</insert>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		
		sb.append(BR2+_T);		
		sb.append("<update id=\"update\" parameterType=\"Object\">");
		sb.append(BR+T2);
		sb.append(" update  "+tabnaeme);
		sb.append(" set  ");
		int j=0;
		for(Field fe:fs){
			if(fe.getName().equals("id"))
				continue;
			sb.append(BR+T2);
			if(j>0){
				sb.append(",");
			}
			sb.append(fe.getName().toLowerCase()+" = #{"+fe.getName()+"}");
			j++;
		}
		sb.append(BR+T2+" where id=#{id}");
		
		sb.append(BR+_T);
		sb.append("</update>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		sb.append(BR2+_T);		
		sb.append("<update id=\"updateNNByID\" parameterType=\"Object\">");
		sb.append(BR+T2);
		sb.append(" update  "+tabnaeme);
		sb.append(" set  ");
		int x=0;
		for(Field fe:fs){
			if(fe.getName().equals("id"))
				continue;
			sb.append(BR+T2);
			if(x>0){
				sb.append(",");
			}
			sb.append("<if test=\"name != null  \">");
			sb.append(BR+T3);
			sb.append(fe.getName().toLowerCase()+" = #{"+fe.getName()+"}");
			sb.append(BR+T2);
			sb.append("</if>");
			x++;
		}
		sb.append(BR+T2+" where id=#{id}");
		
		
		
		sb.append(BR+_T);
		sb.append("</update>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		sb.append(BR2+_T);		
		sb.append("<select id=\"queryById\" parameterType=\"Object\" resultType=\""+clzz.getName()+"\" >");
		sb.append(BR+T2);
		//sb.append("<![CDATA[");
		sb.append("<include refid=\"selectAllColumns\"/>");
		sb.append(BR+T2);
		sb.append("where id = #{id}");
		sb.append(BR+T2);
		sb.append(BR+_T);
		sb.append("</select>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		sb.append(BR2+_T);		
		sb.append("<delete id=\"delete\" parameterType=\"Object\">");
		sb.append(BR+T2);
		sb.append("delete from "+tabnaeme+" where id = #{id}");
		sb.append(BR+_T);
		sb.append("</delete>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		
		
		sb.append(BR2+_T);		
		sb.append("<select id=\"queryCount\" parameterType=\"java.lang.Long\">");
		sb.append(BR+T2);
		//sb.append("<![CDATA[");
		sb.append("select count(1) from "+tabnaeme);
		sb.append(BR+T2);
		sb.append("<include refid=\"where_fragement\" />");
		sb.append(BR+T2);
		sb.append(BR+_T);
		sb.append("</select>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		sb.append(BR2+_T);		
		sb.append("<select id=\"queryList\" parameterType=\"Object\" resultType=\""+clzz.getName()+"\"  >");
		sb.append(BR+T2);
		//sb.append("<![CDATA[");
		sb.append("<include refid=\"selectAllColumns\"/>");
		sb.append(BR+T2);
		sb.append("<include refid=\"where_fragement\" />");
		sb.append(BR+T2);
		sb.append("<include refid=\"common.pagination_tail\" />");
		sb.append(BR+T2);
		sb.append(BR+_T);
		sb.append("</select>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		sb.append(BR2+_T);		
		sb.append("<select id=\"queryByObject\" parameterType=\"Object\" resultType=\""+clzz.getName()+"\"  >");
		sb.append(BR+T2);
		//sb.append("<![CDATA[");
		sb.append("<include refid=\"selectAllColumns\"/>");
		sb.append(BR+T2);
		sb.append("<include refid=\"where_fragement\" />");
		sb.append(BR+T2);
		sb.append(BR+_T);
		sb.append("</select>");
		sb.append(BR+_T);
		sb.append(BR2);
		
		
		
		
		
		
		sb.append("</mapper>");
		FileOutputStream fo=new FileOutputStream(xmlfile);
		
		if(!xmlfile.exists()){
			
			xmlfile.createNewFile();
			System.out.println("创建新文件");
		}else{
			System.out.println("已经存在");
		}
		System.out.println(xmlfile.getAbsolutePath());
		fo.write(sb.toString().getBytes());
		fo.flush();
		fo.close();
	}
}
